package com.srp.backend.controller;

import com.srp.backend.service.PdfProcessingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/pdf")
public class PdfController {

    @Autowired
    private PdfProcessingService pdfProcessingService;

    /**
     * PDF 解析接口
     * @param filePath 已上传 PDF 文件在服务器的路径
     * @return Map<String,Object> Json格式解析结果
     */
    @PostMapping("/parse")
    public ResponseEntity<Map<String, Object>> parsePdf(
            @RequestParam("filePath") String filePath,
            @RequestParam(value = "exportFormat", defaultValue = "word") String exportFomat){
        try{
            Map<String,Object> result = pdfProcessingService.processPdf(filePath, exportFomat);
            return ResponseEntity.ok(result);
        }catch(Exception e){
            //解析过程发生严重异常时返回500
            return ResponseEntity.status(500)
                    .body(Map.of(
                            "status","FAILED",
                            "content","",
                            "error",e.getMessage()
                    ));
        }
    }
}
